package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSON;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.*;
import com.example.exception.CustomException;
import com.example.mapper.CourseMapper;
import com.example.mapper.AttendanceMapper;
import com.example.mapper.StudentMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 考勤信息表业务处理
 **/
@Service
public class AttendanceService {

    @Resource
    private AttendanceMapper attendanceMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private TeacherService teacherService;
    @Resource
    private ClassesService classesService;

    /**
     * 新增
     */
    public void add(Attendance attendance) {
        // 判断同一个学生同一门课同一天的考勤记录只能是一条
        Attendance dbAttendance = attendanceMapper.selectByStudentIdAndCourseIdAndTime(attendance.getStudentId(), attendance.getCourseId(), attendance.getTime());
        if (ObjectUtil.isNotEmpty(dbAttendance)) {
            throw new CustomException(ResultCodeEnum.ATTENDANCE_ALREADY_ERROR);
        }
        attendanceMapper.insert(attendance);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        attendanceMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            attendanceMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Attendance attendance) {
        attendanceMapper.updateById(attendance);
    }

    /**
     * 根据ID查询
     */
    public Attendance selectById(Integer id) {
        return attendanceMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Attendance> selectAll(Attendance attendance) {
        return attendanceMapper.selectAll(attendance);
    }

    /**
     * 分页查询
     */
    public PageInfo<Attendance> selectPage(Attendance attendance, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (RoleEnum.TEACHER.name().equals(currentUser.getRole())) {
            attendance.setTeacherId(currentUser.getId());
            Classes classes = classesService.selectByTeacherId(currentUser.getId());
            attendance.setClassesId(classes.getId());
        }
        if (RoleEnum.STUDENT.name().equals(currentUser.getRole())) {
            attendance.setStudentId(currentUser.getId());
        }
        if (RoleEnum.LEADER.name().equals(currentUser.getRole())) {
            Student student = studentMapper.selectById(currentUser.getId());
            attendance.setClassesId(student.getClassId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Attendance> list = attendanceMapper.selectAll(attendance);
        return PageInfo.of(list);
    }

    /**
     * 根据老师id查询所带班级学生的考勤情况
     * @param teacherId
     * @return
     */
    public  List<Map<String,String>> selectAttendanceByTeacherId(Integer teacherId) {
        List<Map<String,String>> map = attendanceMapper.selectAttendanceByTeacherId(teacherId);

        return map;
    }

    public List<Map<String, String>> selectAttendanceByMonitorId(Integer monitorId) {
        return attendanceMapper.selectAttendanceByMonitorId(monitorId);
    }

    public List<Map<String, String>> selectAttendanceByStudentId(Integer studentId) {
        return attendanceMapper.selectAttendanceByStudentId(studentId);
    }

    public List<Map<String, String>> getClassAttend(Integer studentId) {
        Student student = studentMapper.selectById(studentId);
        return attendanceMapper.getClassAttend(student.getClassId());
    }

    public List<Map<String, String>> getClassmateByCourseId(Integer classId, String courseName) {
        return attendanceMapper.getClassmateByCourseId(classId,courseName);
    }

    public void insert(List<Integer> ids, String courseName,String status) {
        Course course = courseMapper.selectByName(courseName);
        Integer teacherId = course.getTeacherId();
        Integer courseId = course.getId();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String time = format.format(new Date());
        for (Integer id : ids) {
            Attendance attendance = new Attendance();
            attendance.setStudentId(id);
            attendance.setTeacherId(teacherId);
            attendance.setCourseId(courseId);
            attendance.setStatus(status);
            attendance.setTime(time);
            Attendance dbAttendance =attendanceMapper.selectByTime(id,time,courseId);
            if (!ObjectUtil.isNotEmpty(dbAttendance)){
                attendanceMapper.insert(attendance);
                System.out.println("插入成功==" + attendance.getStudentId());
            }
        }
    }
}
